The ZIP is a normally used file format to archive files with information compression. When you need to enable the client to download numerous folders and files from the server, you have to make a ZIP file on the fly. It compresses files and archive to download numerous files at once. The ZIP file creation from the PHP script is basic as like the add to archive functionality in the System.
Making ZIP archive from the directory can be effectively executed utilizing PHP. The ZipArchive class in PHP gives a moment capacity to compress files or organizer in the ZIP file. You can archive the whole directory recursively to ZIP file utilizing PHP. In this instructional exercise, we will demonstrate to you best practices to make ZIP file from folder utilizing PHP.
Class for ZipArchiver:
The ZipArchiver class makes ZIP file from folder (files and sub-folders) on the server with the help of PHP ZipArchive.
Create ZipArchiver.class.php file and put below code:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php Class ZipArchiver { /** * Zip a folder (including itself). * * Usage: * Folder path that should be zipped. * * @param $sourcePath string * Relative path of directory to be zipped. * * @param $outZipPath string * Path of output zip file. * */ public static function zipDir($sourcePath, $outZipPath){ $pathInfo = pathinfo($sourcePath); $parentPath = $pathInfo['dirname']; $dirName = $pathInfo['basename']; $z = new ZipArchive(); $z->open($outZipPath, ZipArchive::CREATE); $z->addEmptyDir($dirName); if($sourcePath == $dirName){ self::dirToZip($sourcePath, $z, 0); }else{ self::dirToZip($sourcePath, $z, strlen("$parentPath/")); } $z->close(); return true; } /** * Add files and sub-directories in a folder to zip file. * * @param $folder string * Folder path that should be zipped. * * @param $zipFile ZipArchive * Zip file where files end up. * * @param $exclusiveLength int * Number of text to be excluded from the file path. * */ private static function dirToZip($folder, &$zipFile, $exclusiveLength){ $handle = opendir($folder); while(FALSE !== $f = readdir($handle)){ // Check for local/parent path or zipping file itself and skip if($f != '.' && $f != '..' && $f != basename(__FILE__)){ $filePath = "$folder/$f"; // Remove prefix from file path before add to zip $localPath = substr($filePath, $exclusiveLength); if(is_file($filePath)){ $zipFile->addFile($filePath, $localPath); }elseif(is_dir($filePath)){ // Add sub-directory $zipFile->addEmptyDir($localPath); self::dirToZip($filePath, $zipFile, $exclusiveLength); } } } closedir($handle); } } |
zipDir() – this function helps to makes Zip of a folder recursively including the parent directory.
$sourcePath – Relative way of the directory to be zipped.
$outZipPath – output zip file Path .
dirToZip() – It is a helper function of class that include files and sub-directories in a folder to zip file.
Now Create ZIP in PHP
we will use ZipArchiver class to archive all files and sub-directories of the given folder and make ZIP file from the script in PHP.
- Include ZipArchive class and initialize it.
- Describe the path of the directory which you need to archive as a ZIP.
- Describe the path to save or store the ZIP file on the server.
- Call the zipDir() capacity of ZipArchiver class to make ZIP.
Create index.php file and write below code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Include and initialize ZipArchive class require_once 'ZipArchiver.class.php'; $zipper = new ZipArchiver; // Path of the directory to which you need to be zipped $dirPath = '/path/to/sourceDir'; // Path of the directory where you want to store output zip file $zipPath = '/path/to/archive-'.date('dmY').'-'.time().'.zip'; // Create zip archive $zip = $zipper->zipDir($dirPath, $zipPath); if($zip){ echo 'ZIP archive created successfully.'; }else{ echo 'Failed to create ZIP.'; } |
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co